var style="";
var lines=8;
var lines_week=10;
var lines_month=5;
var mydialog=null;
var year;
var month;
var day;
var week;
var option={
		 dateFormat:"yy-mm-dd",
		 showOn: "button",
		 buttonImage: "../../../Public/images/date_16x16.gif",
		 buttonImageOnly: true,
		 changeMonth:true,
		 changeYear:true
};
$(function(){
	setTimes();
	setTitle();
	showLines();
	$('#sort').change(function(){
		var num=$(this).val();
		if(num==='1'){
			showLines();
		}else if(num==='2'){
			showLines_week();
		}else if(num==='3'){
			showLines_month();
		}
	});
	
});

function setLines(num){
	lines=num;
}

function showLines(){
	$("#lines").html("");
	for(var j=1;j<=lines;j++){
		$("#lines").append("<div class='line'>"+j+".</div>");
	}
	var obj=$(".line").get();
	var i=0;
	$.each(obj,function(key,value){
	if(i%2==0){
		$(this).css("background-color","#CBE1FC");
	}
	i++;
	});
	$(".line").hover(function(){
		style=$(this).css("background-color");
		$(this).css("background-color","#FFFF99");
	},function(){
		$(this).css("background-color",style);
	});
	
	$(".line").dblclick(function(){
		mydialog=art.dialog({
			title:'請稍候...',
			lock:true,
			ok:function(){}
		});
		$.ajax({
			type:'post',
			url:'plan_content',
			dataType:'html',
			success:function(data){
				mydialog.content(data).title("行事曆");
			    $( "#work_start" ).datepicker({
					 dateFormat:"yy-mm-dd",
					 showOn: "button",
					 buttonImage: "../../../Public/images/date_16x16.gif",
					 buttonImageOnly: true,
			        changeMonth: true,
			        numberOfMonths: 1,
			        onClose: function( selectedDate ) {
			          $( "#work_end" ).datepicker( "option", "minDate", selectedDate );
			        }
			      });
			      $( "#work_end" ).datepicker({
			 		 dateFormat:"yy-mm-dd",
					 showOn: "button",
					 buttonImage: "../../../Public/images/date_16x16.gif",
					 buttonImageOnly: true,
			        changeMonth: true,
			        numberOfMonths: 1,
			        onClose: function( selectedDate ) {
			          $( "#work_start" ).datepicker( "option", "maxDate", selectedDate );
			        }
			      });
			}
		});
	});
}

function setTimes(){
	var mytime=new Date();
	year = mytime.getFullYear();
	for(var i=2000;i<=year+5;i++){
		if(i===year){
			$("#year").append("<option value="+i+" selected='selected'>"+i+"</option>");
		}else
			$("#year").append("<option value="+i+">"+i+"</option>");
	}
	
	month=mytime.getMonth()+1;
	for(var i=1;i<=12;i++){
		if(i===month){
			$("#month").append("<option value="+i+" selected='selected'>"+i+"</option>");
		}else
			$("#month").append("<option value="+i+">"+i+"</option>");
	}
	
	day=mytime.getDate();
	for(var i=1;i<=30;i++){
		if(i===day){
			$("#day").append("<option value="+i+" selected='selected'>"+i+"</option>");
		}else
			$("#day").append("<option value="+i+">"+i+"</option>");
	}
	week=mytime.getDay();
	
}

function setTitle(){
	var weeks=week_deal(week);
	var time="<span id='years'>"+year+"</span>年<span id='months'>"+month+"</span>月<span id='days'>"+day+'</span>日'+'&nbsp;'+"星期<span id='weeks'>"+weeks+"</span>";
	$("#line_title").html(time);
}

function week_deal(num){
	if(num===0){
		return "天";
	}else if(num===1){
		return "一";
	}else if(num===2){
		return "二";
	}else if(num===3){
		return "三";
	}else if(num===4){
		return "四";
	}else if(num===5){
		return "五";
	}else if(num===6){
		return "六";
	}
}

function showLines_week(){
	$("#lines").html("");
	$('#lines').append("<table id='lines_week'></table>");
	if(week!=1){
		
	}
	$("#lines_week").append("<tr><th>時間</th><th>"+month+"月"+day+"日("+week_deal(week)+")</th><th>"+month+"月"+(day+1)+"日("+week_deal(week+1)+")</th><th>"+month+"月"+(day+2)+"日("+week_deal(week+2)+")</th><th>"+month+"月"+(day+3)+"日("+week_deal(week+3)+")</th><th>"+month+"月"+(day+4)+"日("+week_deal(week+4)+")</th><th>"+month+"月"+(day+5)+"日("+week_deal(week+5)+")</th><th>"+month+"月"+(day+6)+"日("+week_deal(week-6)+")</th></tr>");
	var i=8;
	for(var j=0;j<lines_week;j++){
		var time1=""+i;
		var time2=""+(i+1);
		if(i<10){
			time1="0"+i;
		}
		if(i+1<10){
			time2="0"+(i+1);
		}
		$("#lines_week").append("<tr id=tr"+j+">");
		$("#lines_week #tr"+j).append("<td>"+time1+":00"+"~"+time2+":00</td>");
		for(var k=0;k<7;k++){
			$("#lines_week #tr"+j).append("<td class='line'></td>");
		}
		i++;
		$("#lines_week").append("</tr>");
		if(j%2==0){
			$("#lines_week #tr"+j+' td').css("background-color","#CBE1FC");
		}
	}

	$(".line").hover(function(){
		style=$(this).css("background-color");
		$(this).css("background-color","#FFFF99");
	},function(){
		$(this).css("background-color",style);
	});
	
	$(".line").dblclick(function(){
		mydialog=art.dialog({
			title:'提示',
			content:'建設中...敬請期待',
			lock:true,
			ok:function(){}
		});
	});
}

function showLines_month(){
	$("#lines").html("");
	$("#lines").append("<table id='lines_year'><tr><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th><th>星期天</th></tr></table>");
	for(var j=0;j<lines_month;j++){
		$("#lines_year").append("<tr id='tr"+j+"'>");
		for(var i=0;i<7;i++){
			$("#lines_year #tr"+j).append("<td class='line'>j</td>");
		}
		$("#lines_year").append("</tr>");
	}
	var obj=$(".line").get();
	var i=0;
	$.each(obj,function(key,value){
	if(i%2==0){
		$(this).css("background-color","#CBE1FC");
	}
	i++;
	});
	$(".line").hover(function(){
		style=$(this).css("background-color");
		$(this).css("background-color","#FFFF99");
	},function(){
		$(this).css("background-color",style);
	});
	
	$(".line").dblclick(function(){
		mydialog=art.dialog({
			title:'提示',
			content:'建設中...敬請期待',
			lock:true,
			ok:function(){}
		});
	});
}

//---------------------------------

$(function() {
	 var option={
			 dateFormat:"yy-mm-dd",
			 showOn: "button",
			 buttonImage: "../../public/images/date_16x16.gif",
			 buttonImageOnly: true,
			 changeMonth:true,
			 changeYear:true
	 };
	 $( "#cu_birthday" ).datepicker(option); 
	 $( "#cu_birthday2" ).datepicker(option); 
	 $( "#cu_created_at" ).datepicker(option);  
	 $( "#c_10_2" ).datepicker(option);
});
$(function() {
	 var option={
			 dateFormat:"yy-mm-dd",
			 showOn: "button",
			 buttonImage: "../../public/images/date_16x16.gif",
			 buttonImageOnly: true,
			 changeMonth:true,
			 changeYear:true
	 };
	 $( "#cu_birthday" ).datepicker(option); 
	 $( "#cu_birthday2" ).datepicker(option); 
	 $( "#cu_created_at1" ).datepicker(option);  
	 $( "#c_10_2" ).datepicker(option);
});
$(function() {
	 var option={
			 dateFormat:"yy-mm-dd",
			 showOn: "button",
			 buttonImage: "../../public/images/date_16x16.gif",
			 buttonImageOnly: true,
			 changeMonth:true,
			 changeYear:true
	 };
	 $( "#cu_birthday" ).datepicker(option); 
	 $( "#cu_birthday2" ).datepicker(option); 
	 $( "#cu_created_at2" ).datepicker(option);  
	 $( "#c_10_2" ).datepicker(option);
});
var n=15;
var e=1;
function addEle_homeztel(){
	if(n==17){
		alert("最多只能添加3個哦");
		return;
	}
	$("#home_tel").append("<li id="+n+"><input type='text' class='colspan_input'  name='cu_htel"+e+"'/><a onclick='removeEle_hometel("+n+")' class='deleteitem'><i class='icon-trash'></i></a></li>");
	n++;
	e++;
}

function removeEle_hometel(item){
	$("#"+item).remove();
	$("."+item).remove();
	n--;
}


//物件名稱 start
var nn=15;
var ee=1;
function addWJname(){
	if(nn==17){
		alert("最多只能添加3個哦");
		return;
	}
	$("#WJname").append("<li id="+nn+"><input type='text' class='colspan_input1'  name='cu_htel"+e+"'/><a onclick='removeWJname("+nn+")' class='deleteitem'><i class='icon-trash'></i></a></li>");
	nn++;
	ee++;
}

function removeWJname(item){
	$("#"+item).remove();
	$("."+item).remove();
	nn--;
}
//物件名稱end

//需求表
var w=11;
var q=1;
function addRecord(){
	if(w==13){
		alert("最多只能添加3個哦");
		return;
	}
	$("#record").append("<li id="+w+"><input type='text' class='colspan_input1'  name='cu_htel"+e+"'/><a onclick='removeRecord("+w+")' class='deleteitem'><i class='icon-trash'></i></a></li>");
	w++;
	q++;
}

function removeRecord(item){
	$("#"+item).remove();
	$("."+item).remove();
	w--;
}
//需求表


